home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / eflibpt4.zip / DEMO / DATATYPE / SORTDBLL.PAS < prev    next >
Pascal/Delphi Source File  |  1996-08-18  |  3KB  |  100 lines

  1. { Borland Pascal Extended Function Library - EFLIB (C) Johan Larsson, 1996
  2.   Demonstration; doubly linked structure sorting
  3.  
  4.   EFLIB IS PROTECTED BY THE COPYRIGHT LAW AND MAY NOT BE COPIED, SOLD OR
  5.   MANIPULATED. FOR MORE INFORMATION, SEE PROGRAM MANUAL! THIS DEMONSTRAT-
  6.   ION PROGRAM MAY FREELY BE USED AND DISTRIBUTED.                          }
  7.  
  8.  
  9. uses EFLIBDEF, EFLIBDAT, EFLIBBAS;
  10.  
  11.  
  12. const NumberOfElements = 500;
  13.  
  14. var Structure : LinkedListObjectType; Timer : TimerObjectType;
  15.     Index, Number : word;
  16.  
  17.  
  18. { Add elements }
  19. procedure Build;
  20. begin
  21.      if Structure.IsInitialized then
  22.         for Index := 1 to NumberOfElements do begin
  23.             Number := Succ(Random(High(Number)));
  24.             Structure.Add (Number); { Add element }
  25.         end;
  26. end;
  27.  
  28. procedure TestCommonSorting;
  29. begin
  30.      with Structure do begin
  31.           { Make sure that the structure is empty }
  32.           Clear;
  33.  
  34.           { Disable active sorting }
  35.           SetUnsortedOrder;
  36.  
  37.           Write ('Common sorting : ');
  38.  
  39.           Timer.Initialize;
  40.  
  41.           { Build structure without sorting it }
  42.           Build;
  43.  
  44.           { Sort structure with QuickSort algorithm }
  45.           Sort (AscendingOrder, QuickSortAlgorithm);
  46.  
  47.           WriteLn (Timer.ElapsedMS:0:0, ' [ms]');
  48.  
  49.           Timer.Intercept;
  50.      end;
  51. end;
  52.  
  53.  
  54. procedure TestActiveSorting;
  55. begin
  56.      with Structure do begin
  57.           { Make sure that the structure is empty }
  58.           Clear;
  59.  
  60.           { Setup active sorting }
  61.           SetAscendingOrder; { Ascending sort order }
  62.           SetSearchMethod (BinarySearchMethod); { Use fast binary search }
  63.  
  64.           Write ('Active sorting : ');
  65.  
  66.           Timer.Initialize;
  67.  
  68.           { Active sorting during insertion of new elements }
  69.           Build;
  70.  
  71.           WriteLn (Timer.ElapsedMS:0:0, ' [ms]');
  72.  
  73.           Timer.Intercept;
  74.      end;
  75. end;
  76.  
  77.  
  78. begin
  79.      { EFLIB supplies two ways of sorting doubly linked data structures:
  80.  
  81.         1) common sorting algorithms after the structure is built,
  82.         2) active sorting where each new element is placed in sorted order.
  83.  
  84.        The common sorting can be performed with various sorting algorithms
  85.        as implemented in DataObjectType (the parent object for all EFLIB
  86.        data types). Active sorting can use linear or binary search for
  87.        the the attachment node. Binary searching has supirior performance. }
  88.  
  89.      RandSeed := 0; { Control random seed }
  90.  
  91.      WriteLn ('* Doubly linked structure sorting performance *');
  92.  
  93.      Structure.Initialize (SizeOf(Number));
  94.  
  95.        TestCommonSorting;
  96.        TestActiveSorting;
  97.  
  98.      Structure.Intercept;
  99.      if GlobalDataError then WriteLn ('Error(s) reported!');
  100. end.